package com.pioneer.admin.controller.stock;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.stock.domain.StoDec;
import com.pioneer.web.stock.service.IStoDecService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 库存管理
 *
 * @author xx
 */
@RestController
@RequestMapping("/stock/decrease")
public class StoDecController extends BaseController {

    @Resource
    private IStoDecService stoService;

    /**
     * 获取库存管理列表
     *
     * @param dec 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:list')")
    @GetMapping("/list")
    public AjaxResult list(StoDec dec) {
        boolean isPage = startPage();
        List<StoDec> list = stoService.list(Wrappers.query(dec));
        return success(isPage, list);
    }

    /**
     * 根据库存管理id获取详细信息
     *
     * @param decId 库存管理id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:query')")
    @GetMapping(value = "/{decId}")
    public AjaxResult getInfo(@PathVariable Long decId) {
        return AjaxResult.success(stoService.select(decId));
    }

    /**
     * 新增库存管理
     *
     * @param dec 库存管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:add')")
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StoDec dec) {
        return toAjax(stoService.insertDec(dec));
    }

    /**
     * 修改库存管理
     *
     * @param dec 库存管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:edit')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StoDec dec) {
        return toAjax(stoService.updateDec(dec));
    }

    /**
     * 删除库存管理
     *
     * @param decIds 库存管理ids组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:remove')")
    @Log(title = "库存管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{decIds}")
    public AjaxResult remove(@PathVariable Long[] decIds) {
        return toAjax(stoService.deleteStoDec(Arrays.asList(decIds)));
    }

    /**
     * 确认损耗
     *
     * @param stoDec 损耗
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:decrease:approve')")
    @Log(title = "库存管理", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@Validated @RequestBody StoDec stoDec) {
        return toAjax(stoService.approveStoDec(stoDec));
    }

}
